package com.drivingtheproject.wifie.data;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.drivingtheproject.wifie.types.Mode;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Saurabh Dubey on 15.08.2014.
 */
public class DatabaseHandler extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "WiFiExpert";

    // Contacts table name
    private static final String TABLE_PROFILES = "profiles";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_SSID = "ssid";
    private static final String KEY_MODE= "mode";
    private static final String KEY_SYNC_ON= "sync_on";
    private static final String KEY_LOCATION_ON = "location_on";
    private static final String KEY_MOBILE_DATA_ON = "mobile_data_on";
    private static final String KEY_POWER_SAVING_ON = "power_saving_on";
    private static final String KEY_BLUETOOTH_ON = "bluetooth_on";
    private static final String KEY_ENABLE_ON = "enable_on";
    private static final String KEY_MESSAGE_ON = "message_on";
    private static final String KEY_MESSAGE = "message";
    private static final String KEY_CONTACT_NUMBER = "contact_no";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_PROFILES + "("
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + KEY_SSID + " TEXT,"
                + KEY_NAME + " TEXT,"
                + KEY_MODE + " INTEGER,"
                + KEY_SYNC_ON + " INTEGER,"
                + KEY_LOCATION_ON + " INTEGER,"
                + KEY_MOBILE_DATA_ON + " INTEGER,"
                + KEY_POWER_SAVING_ON + " INTEGER,"
                + KEY_BLUETOOTH_ON + " INTEGER,"
                + KEY_ENABLE_ON + " INTEGER,"
                + KEY_MESSAGE_ON + " INTEGER,"
                + KEY_MESSAGE + " TEXT,"
                + KEY_CONTACT_NUMBER + " TEXT"
                + ")";
        sqLiteDatabase.execSQL(CREATE_CONTACTS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
        // Drop older table if existed
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_PROFILES);
        // Create tables again
        onCreate(sqLiteDatabase);
    }

    public long addProfile(Profile profile)
    {
        long result = 0;
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(KEY_NAME, profile.getName());
        values.put(KEY_SSID, profile.getSSID());
        values.put(KEY_MODE, profile.getMode().value());
        values.put(KEY_SYNC_ON, profile.isSyncOn()?1:0);
        values.put(KEY_LOCATION_ON, profile.isLocationOn()?1:0);
        values.put(KEY_MOBILE_DATA_ON, profile.isMobileDataOn()?1:0);
       // values.put(KEY_POWER_SAVING_ON, profile.isPowerSavingOn()?1:0);
        values.put(KEY_BLUETOOTH_ON, profile.isBluetoothOn()?1:0);

        // Inserting Row
        result=db.insert(TABLE_PROFILES, null, values);
        db.close();

        return result;
    }

    public int updateProfile(Profile profile)
    {
        int result = 0;
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, profile.getName());
        values.put(KEY_SSID, profile.getSSID());
        values.put(KEY_MODE, profile.getMode().value());
        values.put(KEY_SYNC_ON, profile.isSyncOn());
        values.put(KEY_LOCATION_ON, profile.isLocationOn());
        values.put(KEY_MOBILE_DATA_ON, profile.isMobileDataOn());
        //values.put(KEY_POWER_SAVING_ON, profile.isPowerSavingOn());
        values.put(KEY_BLUETOOTH_ON, profile.isBluetoothOn());

        // Updating Row
        result=db.update(TABLE_PROFILES, values, KEY_ID + " = ?",
                new String[]{String.valueOf(profile.getId())});
        db.close();
        return result;
    }

    public int deleteProfile(int id)
    {
        int result = 0;
        SQLiteDatabase db = this.getWritableDatabase();
        result=db.delete(TABLE_PROFILES, KEY_ID + " = ?",
                new String[] { String.valueOf(id) });
        db.close();
        return result;
    }

    public ArrayList<Profile> getProfiles()
    {
        ArrayList<Profile> result = new ArrayList<Profile>() ;

        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_PROFILES;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Profile profile = new Profile();
                profile.setId(Integer.parseInt(cursor.getString(0)));
                profile.setSSID(cursor.getString(1));
                profile.setName(cursor.getString(2));

                profile.setMode(Mode.fromInteger(Integer.parseInt(cursor.getString(3))));
                profile.setSyncOn(Boolean.parseBoolean(cursor.getString(4)));
                profile.setLocationOn(Boolean.parseBoolean(cursor.getString(5)));
                profile.setMobileDataOn(Boolean.parseBoolean(cursor.getString(6)));
                // profile.setPowerSavingOn(Boolean.parseBoolean(cursor.getString(7)));
                profile.setBluetoothOn(Boolean.parseBoolean(cursor.getString(8)));
                // Adding contact to list
                result.add(profile);
            } while (cursor.moveToNext());
        }

        return result;
    }
}
